﻿using Ops.Host.Common.Extensions;
using Ops.Host.Core.Models;

namespace Ops.Host.Core.Services.Impl;

internal sealed class EnergyConsumptionService : IEnergyConsumptionService
{
    private readonly IFreeSql _freeSql;

    public EnergyConsumptionService(IFreeSql freeSql) => _freeSql = freeSql;

    public (List<EnergyConsumption> items, long count) GetPaged(EnergyConsumptionFilter filter, int pageIndex, int pageItems)
    {
        var items = _freeSql.Select<EnergyConsumption>()
                            .WhereIf(!string.IsNullOrEmpty(filter.Station), s => s.Station == filter.Station)
                            .WhereIf(filter.CreatedTimeStart != null, s => s.CreatedAt >= filter.CreatedTimeStart.ToDayMin())
                            .WhereIf(filter.CreatedTimeEnd != null, s => s.CreatedAt <= filter.CreatedTimeEnd.ToDayMax())
                            .OrderByDescending(s => s.CreatedAt)
                            .Count(out var total) //总记录数量
                            .Page(pageIndex, pageItems)
                            .ToList();
        return (items, total);
    }
}
